<?php
//
namespace app\circle\controller\admin;

use app\circle\model\CircleModel;
use app\circle\model\MeetingModel;
use app\circle\model\MeetingReceiveLogModel;
use app\circle\model\MeetingReceiveModel;
use think\Request;
use think\Validate;

class MeetingController extends BaseController
{

    private $circleModel;
    private $meetingModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->meetingModel = new MeetingModel();
        $this->circleModel = new CircleModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $map = [];
        $query = $this->request->request();
        $meetingName = $this->request->param('meeting_name');
        if (!empty($meetingName)) {
            $map['meeting_name'] = ['like', '%' . $meetingName . '%'];
        }
        if ($this->adminInfo['admin_power'] == 2) {
            $map['dept_id'] = $this->adminInfo['dept_id'];
        }
        if ($this->adminInfo['admin_power'] == 3) {
            $map['admin_id'] = $this->adminInfo['admin_id'];
        }
        $list = $this->meetingModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $result = $this->validate($data, [
                'meeting_name|会议名称' => 'require',
                'circle_id|绑定圈子' => 'require|>:0',
                'meeting_place|会议地点' => 'require',
                'meeting_number|参会人数' => 'require',
                'start_time|会议开始时间' => 'date',
                'end_time|会议结束时间' => 'date',
                'sign_start_time|报名开始时间' => 'date',
                'sign_end_time|报名结束时间' => 'date',
                'meeting_describe|会议主题词' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $circleInfo = $this->circleModel->where('circle_id', $data['circle_id'])->find();
            if (empty($circleInfo)) {
                $this->error('绑定圈子不存在！');
            }
            $data['circle_name'] = $circleInfo['circle_name'];
            $data['content'] = $this->formatContent($data);
            $data['dept_id'] = $this->adminInfo['dept_id'];
            $data['dept_name'] = $this->adminInfo['dept_name'];
            $data['dept_path'] = $this->adminInfo['dept_path'];
            $data['admin_id'] = $this->adminInfo['admin_id'];
            $this->meetingModel->addData($data);
            $this->success('操作成功！', 'index');
        }
        $map = [];
        if ($this->adminInfo['admin_power'] == 2) {
            $map['dept_id'] = $this->adminInfo['dept_id'];
        }
        if ($this->adminInfo['admin_power'] == 3) {
            $map['admin_id'] = $this->adminInfo['admin_id'];
        }
        $circle = $this->circleModel->where($map)->select();
        $this->assign('circle', $circle);
        return $this->fetch();
    }

    public function formatContent($data)
    {
        $content['meeting_sponsor'] = $data['meeting_sponsor']; //主办
        $content['meeting_organizer'] = $data['meeting_organizer']; //承办
        $content['meeting_process'] = $data['meeting_process']; //参会流程
        $content['meeting_content'] = $data['meeting_content']; //会议内容
        $content['meeting_contact'] = $data['meeting_contact']; //联系方式
        return $content;
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $result = $this->validate($data, [
                'meeting_name|会议名称' => 'require',
                'circle_id|绑定圈子' => 'require|>:0',
                'meeting_place|会议地点' => 'require',
                'meeting_number|参会人数' => 'require',
                'start_time|会议开始时间' => 'date',
                'end_time|会议结束时间' => 'date',
                'sign_start_time|报名开始时间' => 'date',
                'sign_end_time|报名结束时间' => 'date',
                'meeting_describe|会议主题词' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $circleInfo = $this->circleModel->where('circle_id', $data['circle_id'])->find();
            if (empty($circleInfo)) {
                $this->error('绑定圈子不存在！');
            }
            $data['circle_name'] = $circleInfo['circle_name'];
            $data['content'] = $this->formatContent($data);
            $this->meetingModel->editData($data);
            $this->success('操作成功！', 'index');
        }
        $meetingId = $this->request->param('meeting_id/d');
        $info = $this->meetingModel->where('meeting_id', $meetingId)->find();
        $map = [];
        if ($this->adminInfo['admin_power'] == 2) {
            $map['dept_id'] = $this->adminInfo['dept_id'];
        }
        if ($this->adminInfo['admin_power'] == 3) {
            $map['admin_id'] = $this->adminInfo['admin_id'];
        }
        $circle = $this->circleModel->where($map)->select();
        $this->assign('circle', $circle);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del()
    {
        $meetingId = $this->request->param('meeting_id/d');
        $this->meetingModel->where('meeting_id', $meetingId)->delete();
        $this->success('删除成功！');
    }

    /*会议领取商品添加*/
    public function receive()
    {
        $meetingId=$this->request->param('meeting_id',0);
        $model=new MeetingReceiveModel();
        $list=$model->where('meeting_id',$meetingId)->select();
        if ($this->request->isPost()){
            return $list;
        }
        $this->assign('meeting_id',$meetingId);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function receiveAdd()
    {
        $meetingId=$this->request->param('meeting_id',0);
        $meetingInfo=$this->meetingModel->where('meeting_id',$meetingId)->find();
        if ($this->request->isPost()){
            $data=$this->request->param();
            $validate=new Validate([
                ['receive_name','require','请输入领取名称'],
                ['receive_content','require','请输入领取的简介'],
                ['meeting_id','require','会议不明确']
            ]);
            if (!$validate->check($data)){
                $this->error($validate->getError());
            }
            $data+=[
                'circle_id'=>$meetingInfo['circle_id'],
                'dept_id'=>$meetingInfo['dept_id']
            ];
            $model=new MeetingReceiveModel();
            if ($model->addData($data)){
                $this->success('添加成功',url('receive',['meeting_id'=>$meetingId]));
            }else{
                $this->error($model->getError());
            }
        }
        $this->assign('meetingInfo',$meetingInfo);
        return $this->fetch();
    }

    public function receiveEdit()
    {
        $receiveId=$this->request->param('receive_id',0);
        $model=new MeetingReceiveModel();
        $info=$model->where('receive_id',$receiveId)->find();
        $meetingInfo=$this->meetingModel->where('meeting_id',$info['meeting_id'])->find();
        if ($this->request->isPost()){
            $data=$this->request->param();
            $validate=new Validate([
                ['receive_name','require','请输入领取名称'],
                ['receive_content','require','请输入领取的简介'],
                ['meeting_id','require','会议不明确'],
                ['receive_id','require','修改对象不明确']
            ]);
            if (!$validate->check($data)){
                $this->error($validate->getError());
            }
            $data+=[
                'circle_id'=>$meetingInfo['circle_id'],
                'dept_id'=>$meetingInfo['dept_id']
            ];
            $model=new MeetingReceiveModel();
            if ($model->editData($data)){
                $this->success('添加成功',url('receive',['meeting_id'=>$data['meeting_id']]));
            }else{
                $this->error($model->getError());
            }
        }
        $this->assign('meetingInfo',$meetingInfo);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function receiveDel()
    {
        $receiveId=$this->request->param('receive_id',0);
        $model=new MeetingReceiveModel();
        if ($model->where('receive_id',$receiveId)->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        };
    }

    public function receiveLog()
    {
        $receiveId=$this->request->param('receive_id',0);
        $model=new MeetingReceiveLogModel();
        $map=['receive_id'=>$receiveId];
        $list=$model->where($map)->paginate();
        $this->assign('list',$list);
        return $this->fetch();
    }

}
